<?php

namespace App\Admin\Controllers\Datashow;

use App\Http\Controllers\Chanjet\BaseController;
use App\Http\Controllers\Chanjet\TplusCommonController;
use App\Models\Company;
use Illuminate\Http\Request;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redis;
use App\Models\Purchase\PurchaseOrderSub;
use App\Models\Purchase\PurchaseStockinOrderSub;
use App\Models\Purchase\PurchaseWarehousingOrderSub;


use App\Models\Order\Outboundordersub;
use App\Models\Order\Saleordersub;
use App\Models\Order\Salesordersub;

class DatashowController extends Controller
{
    /**
     * 根据需求查询相关数据
     */
    public function showdata() {}


    public function purchaseshowdata(Request $request)
    {
        $cpid = $request->cpid;
        $start_time = strtotime($request->start_time) > 0 ? strtotime($request->start_time) : intval($request->start_time);
        $end_time = strtotime($request->end_time) > 0 ? strtotime($request->end_time) : intval($request->end_time);

        if ($start_time > 0 && $end_time > $start_time) {
            $today = [
                'start_time' => $start_time,
                'end_time' => $end_time
            ];
            $index1 = [];

            $index1['today'] = PurchaseOrderSub::with(['mainorder'])->where(function ($query) use ($cpid) {
                $cpid > 0 && $query->where('cpid', $cpid);
            })->whereHas('mainorder', function ($query) use ($today) {
                $query->where('is_del', 1)->where([
                    ['create_at', '>=', $today['start_time']],
                    ['create_at', '<=', $today['end_time']],
                ]);
            })->where('is_del', 1)->sum('total_weight');



            $index2 = [];

            $index2['today'] = PurchaseOrderSub::with(['mainorder'])->where(function ($query) use ($cpid) {
                $cpid > 0 && $query->where('cpid', $cpid);
            })->whereHas('mainorder', function ($query) use ($today) {
                $query->where('is_del', 1)->where([
                    ['create_at', '>=', $today['start_time']],
                    ['create_at', '<=', $today['end_time']],
                ]);
            })->where('is_del', 1)->sum('total_money');




            $index3 = [];

            $index3['today'] = PurchaseStockinOrderSub::with(['mainorder'])->where(function ($query) use ($cpid) {
                $cpid > 0 && $query->where('cpid', $cpid);
            })->whereHas('mainorder', function ($query) use ($today) {
                $query->where('is_del', 1)->where([
                    ['create_at', '>=', $today['start_time']],
                    ['create_at', '<=', $today['end_time']],
                ]);
            })->where('is_del', 1)->sum('total_weight');





            $index4 = [];

            $index4['today'] = PurchaseStockinOrderSub::with(['mainorder'])->where(function ($query) use ($cpid) {
                $cpid > 0 && $query->where('cpid', $cpid);
            })->whereHas('mainorder', function ($query) use ($today) {
                $query->where('is_del', 1)->where([
                    ['create_at', '>=', $today['start_time']],
                    ['create_at', '<=', $today['end_time']],
                ]);
            })->where('is_del', 1)->sum('total_money');




            $index5 = [];

            $index5['today'] = PurchaseWarehousingOrderSub::with(['mainorder'])->where(function ($query) use ($cpid) {
                $cpid > 0 && $query->where('cpid', $cpid);
            })->whereHas('mainorder', function ($query) use ($today) {
                $query->where('audit_status', 3)->where('is_del', 1)->where([
                    ['create_at', '>=', $today['start_time']],
                    ['create_at', '<=', $today['end_time']],
                ]);
            })->where('is_del', 1)->sum('total_weight');




            $index6 = [];

            $index6['today'] = PurchaseWarehousingOrderSub::with(['mainorder'])->where(function ($query) use ($cpid) {
                $cpid > 0 && $query->where('cpid', $cpid);
            })->whereHas('mainorder', function ($query) use ($today) {
                $query->where('audit_status', 3)->where('is_del', 1)->where([
                    ['create_at', '>=', $today['start_time']],
                    ['create_at', '<=', $today['end_time']],
                ]);
            })->where('is_del', 1)->sum('total_money');



            $index7 = [];
            $index7['today'] = $index1['today'] > 0 ? mathfuncmoney('div', $index2['today'], $index1['today']) : '';

            $returndata = compact('index1', 'index2', 'index3', 'index4', 'index5', 'index6', 'index7');
        } else {
            $today = [
                'start_time' => mktime(0, 0, 0, date('m'), date('d'), date('Y')),
                'end_time' => time()
            ];
            $yesterday = [
                'start_time' => mktime(0, 0, 0, date('m'), date('d') - 1, date('Y')),
                'end_time' => mktime(0, 0, 0, date('m'), date('d'), date('Y')) - 1
            ];

            $index1 = [];

            $index1['today'] = PurchaseOrderSub::with(['mainorder'])->where(function ($query) use ($cpid) {
                $cpid > 0 && $query->where('cpid', $cpid);
            })->whereHas('mainorder', function ($query) use ($today) {
                $query->where('is_del', 1)->where([
                    ['create_at', '>=', $today['start_time']],
                    ['create_at', '<=', $today['end_time']],
                ]);
            })->where('is_del', 1)->sum('total_weight');

            $index1['yesterday'] = PurchaseOrderSub::with(['mainorder'])->where(function ($query) use ($cpid) {
                $cpid > 0 && $query->where('cpid', $cpid);
            })->whereHas('mainorder', function ($query) use ($yesterday) {
                $query->where('is_del', 1)->where([
                    ['create_at', '>=', $yesterday['start_time']],
                    ['create_at', '<=', $yesterday['end_time']],
                ]);
            })->where('is_del', 1)->sum('total_weight');



            $index2 = [];

            $index2['today'] = PurchaseOrderSub::with(['mainorder'])->where(function ($query) use ($cpid) {
                $cpid > 0 && $query->where('cpid', $cpid);
            })->whereHas('mainorder', function ($query) use ($today) {
                $query->where('is_del', 1)->where([
                    ['create_at', '>=', $today['start_time']],
                    ['create_at', '<=', $today['end_time']],
                ]);
            })->where('is_del', 1)->sum('total_money');

            $index2['yesterday'] = PurchaseOrderSub::with(['mainorder'])->where(function ($query) use ($cpid) {
                $cpid > 0 && $query->where('cpid', $cpid);
            })->whereHas('mainorder', function ($query) use ($yesterday) {
                $query->where('is_del', 1)->where([
                    ['create_at', '>=', $yesterday['start_time']],
                    ['create_at', '<=', $yesterday['end_time']],
                ]);
            })->where('is_del', 1)->sum('total_money');




            $index3 = [];

            $index3['today'] = PurchaseStockinOrderSub::with(['mainorder'])->where(function ($query) use ($cpid) {
                $cpid > 0 && $query->where('cpid', $cpid);
            })->whereHas('mainorder', function ($query) use ($today) {
                $query->where('is_del', 1)->where([
                    ['create_at', '>=', $today['start_time']],
                    ['create_at', '<=', $today['end_time']],
                ]);
            })->where('is_del', 1)->sum('total_weight');

            $index3['yesterday'] = PurchaseStockinOrderSub::with(['mainorder'])->where(function ($query) use ($cpid) {
                $cpid > 0 && $query->where('cpid', $cpid);
            })->whereHas('mainorder', function ($query) use ($yesterday) {
                $query->where('is_del', 1)->where([
                    ['create_at', '>=', $yesterday['start_time']],
                    ['create_at', '<=', $yesterday['end_time']],
                ]);
            })->where('is_del', 1)->sum('total_weight');





            $index4 = [];

            $index4['today'] = PurchaseStockinOrderSub::with(['mainorder'])->where(function ($query) use ($cpid) {
                $cpid > 0 && $query->where('cpid', $cpid);
            })->whereHas('mainorder', function ($query) use ($today) {
                $query->where('is_del', 1)->where([
                    ['create_at', '>=', $today['start_time']],
                    ['create_at', '<=', $today['end_time']],
                ]);
            })->where('is_del', 1)->sum('total_money');

            $index4['yesterday'] = PurchaseStockinOrderSub::with(['mainorder'])->where(function ($query) use ($cpid) {
                $cpid > 0 && $query->where('cpid', $cpid);
            })->whereHas('mainorder', function ($query) use ($yesterday) {
                $query->where('is_del', 1)->where([
                    ['create_at', '>=', $yesterday['start_time']],
                    ['create_at', '<=', $yesterday['end_time']],
                ]);
            })->where('is_del', 1)->sum('total_money');




            $index5 = [];

            $index5['today'] = PurchaseWarehousingOrderSub::with(['mainorder'])->where(function ($query) use ($cpid) {
                $cpid > 0 && $query->where('cpid', $cpid);
            })->whereHas('mainorder', function ($query) use ($today) {
                $query->where('audit_status', 3)->where('is_del', 1)->where([
                    ['create_at', '>=', $today['start_time']],
                    ['create_at', '<=', $today['end_time']],
                ]);
            })->where('is_del', 1)->sum('total_weight');

            $index5['yesterday'] = PurchaseWarehousingOrderSub::with(['mainorder'])->where(function ($query) use ($cpid) {
                $cpid > 0 && $query->where('cpid', $cpid);
            })->whereHas('mainorder', function ($query) use ($yesterday) {
                $query->where('audit_status', 3)->where('is_del', 1)->where([
                    ['create_at', '>=', $yesterday['start_time']],
                    ['create_at', '<=', $yesterday['end_time']],
                ]);
            })->where('is_del', 1)->sum('total_weight');




            $index6 = [];

            $index6['today'] = PurchaseWarehousingOrderSub::with(['mainorder'])->where(function ($query) use ($cpid) {
                $cpid > 0 && $query->where('cpid', $cpid);
            })->whereHas('mainorder', function ($query) use ($today) {
                $query->where('audit_status', 3)->where('is_del', 1)->where([
                    ['create_at', '>=', $today['start_time']],
                    ['create_at', '<=', $today['end_time']],
                ]);
            })->where('is_del', 1)->sum('total_money');

            $index6['yesterday'] = PurchaseWarehousingOrderSub::with(['mainorder'])->where(function ($query) use ($cpid) {
                $cpid > 0 && $query->where('cpid', $cpid);
            })->whereHas('mainorder', function ($query) use ($yesterday) {
                $query->where('audit_status', 3)->where('is_del', 1)->where([
                    ['create_at', '>=', $yesterday['start_time']],
                    ['create_at', '<=', $yesterday['end_time']],
                ]);
            })->where('is_del', 1)->sum('total_money');



            $index7 = [];
            $index7['today'] = $index1['today'] > 0 ? mathfuncmoney('div', $index2['today'], $index1['today']) : '';
            $index7['yesterday'] = $index1['yesterday'] > 0 ? mathfuncmoney('div', $index2['yesterday'], $index1['yesterday']) : '';

            $returndata = compact('index1', 'index2', 'index3', 'index4', 'index5', 'index6', 'index7');
        }


        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $returndata]);
    }


    public function saleshowdata(Request $request)
    {
        $cpid = $request->cpid;
        $start_time = strtotime($request->start_time) > 0 ? strtotime($request->start_time) : intval($request->start_time);
        $end_time = strtotime($request->end_time) > 0 ? strtotime($request->end_time) : intval($request->end_time);

        if ($start_time > 0 && $end_time > $start_time) {
            $today = [
                'start_time' => $start_time,
                'end_time' => $end_time
            ];
            $index1 = [];

            $index1['today'] = Saleordersub::where(function ($query) use ($cpid) {
                $cpid > 0 && $query->where('cpid', $cpid);
            })->where([
                ['create_at', '>=', $today['start_time']],
                ['create_at', '<=', $today['end_time']],
            ])->where('is_del', 1)->sum('total_weight');



            $index2 = [];

            $index2['today'] = Saleordersub::where(function ($query) use ($cpid) {
                $cpid > 0 && $query->where('cpid', $cpid);
            })->where([
                ['create_at', '>=', $today['start_time']],
                ['create_at', '<=', $today['end_time']],
            ])->where('is_del', 1)->sum('total');




            $index3 = [];

            $index3['today'] = Salesordersub::where(function ($query) use ($cpid) {
                $cpid > 0 && $query->where('cpid', $cpid);
            })->where([
                ['create_at', '>=', $today['start_time']],
                ['create_at', '<=', $today['end_time']],
            ])->where('is_del', 1)->sum('weight');





            $index4 = [];

            $index4['today'] = Salesordersub::where(function ($query) use ($cpid) {
                $cpid > 0 && $query->where('cpid', $cpid);
            })->where([
                ['create_at', '>=', $today['start_time']],
                ['create_at', '<=', $today['end_time']],
            ])->where('is_del', 1)->sum('total');




            $index5 = [];

            $index5['today'] = Outboundordersub::where(function ($query) use ($cpid) {
                $cpid > 0 && $query->where('cpid', $cpid);
            })->where([
                ['create_at', '>=', $today['start_time']],
                ['create_at', '<=', $today['end_time']],
            ])->sum('weight');




            $index6 = [];

            $index6['today'] = Outboundordersub::where(function ($query) use ($cpid) {
                $cpid > 0 && $query->where('cpid', $cpid);
            })->where([
                ['create_at', '>=', $today['start_time']],
                ['create_at', '<=', $today['end_time']],
            ])->sum('total');



            $index7 = [];
            $index7['today'] = $index1['today'] > 0 ? mathfuncmoney('div', $index2['today'], $index1['today']) : '';

            $returndata = compact('index1', 'index2', 'index3', 'index4', 'index5', 'index6', 'index7');
        } else {
            $today = [
                'start_time' => mktime(0, 0, 0, date('m'), date('d'), date('Y')),
                'end_time' => time()
            ];
            $yesterday = [
                'start_time' => mktime(0, 0, 0, date('m'), date('d') - 1, date('Y')),
                'end_time' => mktime(0, 0, 0, date('m'), date('d'), date('Y')) - 1
            ];

            $index1 = [];

            $index1['today'] = Saleordersub::where(function ($query) use ($cpid) {
                $cpid > 0 && $query->where('cpid', $cpid);
            })->where([
                ['create_at', '>=', $today['start_time']],
                ['create_at', '<=', $today['end_time']],
            ])->where('is_del', 1)->sum('total_weight');

            $index1['yesterday'] = Saleordersub::where(function ($query) use ($cpid) {
                $cpid > 0 && $query->where('cpid', $cpid);
            })->where([
                ['create_at', '>=', $yesterday['start_time']],
                ['create_at', '<=', $yesterday['end_time']],
            ])->where('is_del', 1)->sum('total_weight');





            $index2 = [];

            $index2['today'] = Saleordersub::where(function ($query) use ($cpid) {
                $cpid > 0 && $query->where('cpid', $cpid);
            })->where([
                ['create_at', '>=', $today['start_time']],
                ['create_at', '<=', $today['end_time']],
            ])->where('is_del', 1)->sum('total');

            $index2['yesterday'] = Saleordersub::where(function ($query) use ($cpid) {
                $cpid > 0 && $query->where('cpid', $cpid);
            })->where([
                ['create_at', '>=', $yesterday['start_time']],
                ['create_at', '<=', $yesterday['end_time']],
            ])->where('is_del', 1)->sum('total');




            $index3 = [];

            $index3['today'] = Salesordersub::where(function ($query) use ($cpid) {
                $cpid > 0 && $query->where('cpid', $cpid);
            })->where([
                ['create_at', '>=', $today['start_time']],
                ['create_at', '<=', $today['end_time']],
            ])->where('is_del', 1)->sum('weight');

            $index3['yesterday'] = Salesordersub::where(function ($query) use ($cpid) {
                $cpid > 0 && $query->where('cpid', $cpid);
            })->where([
                ['create_at', '>=', $yesterday['start_time']],
                ['create_at', '<=', $yesterday['end_time']],
            ])->where('is_del', 1)->sum('weight');





            $index4 = [];

            $index4['today'] = Salesordersub::where(function ($query) use ($cpid) {
                $cpid > 0 && $query->where('cpid', $cpid);
            })->where([
                ['create_at', '>=', $today['start_time']],
                ['create_at', '<=', $today['end_time']],
            ])->where('is_del', 1)->sum('total');

            $index4['yesterday'] = Salesordersub::where(function ($query) use ($cpid) {
                $cpid > 0 && $query->where('cpid', $cpid);
            })->where([
                ['create_at', '>=', $yesterday['start_time']],
                ['create_at', '<=', $yesterday['end_time']],
            ])->where('is_del', 1)->sum('total');




            $index5 = [];

            $index5['today'] = Outboundordersub::where(function ($query) use ($cpid) {
                $cpid > 0 && $query->where('cpid', $cpid);
            })->where([
                ['create_at', '>=', $today['start_time']],
                ['create_at', '<=', $today['end_time']],
            ])->sum('weight');

            $index5['yesterday'] = Outboundordersub::where(function ($query) use ($cpid) {
                $cpid > 0 && $query->where('cpid', $cpid);
            })->where([
                ['create_at', '>=', $yesterday['start_time']],
                ['create_at', '<=', $yesterday['end_time']],
            ])->sum('weight');




            $index6 = [];

            $index6['today'] = Outboundordersub::where(function ($query) use ($cpid) {
                $cpid > 0 && $query->where('cpid', $cpid);
            })->where([
                ['create_at', '>=', $today['start_time']],
                ['create_at', '<=', $today['end_time']],
            ])->sum('total');

            $index6['yesterday'] = Outboundordersub::where(function ($query) use ($cpid) {
                $cpid > 0 && $query->where('cpid', $cpid);
            })->where([
                ['create_at', '>=', $yesterday['start_time']],
                ['create_at', '<=', $yesterday['end_time']],
            ])->sum('total');



            $index7 = [];
            $index7['today'] = $index1['today'] > 0 ? mathfuncmoney('div', $index2['today'], $index1['today']) : '';
            $index7['yesterday'] = $index1['yesterday'] > 0 ? mathfuncmoney('div', $index2['yesterday'], $index1['yesterday']) : '';

            $returndata = compact('index1', 'index2', 'index3', 'index4', 'index5', 'index6', 'index7');
        }


        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $returndata]);
    }

    /**
     *  检查目标查询结果是否有缓存
     *
     */
    public function checkReportCache($data ,$request){
        if(empty($data["controllerName"]) || empty($data["condition"])){
            return ['code'=>'001','msg'=>'请求参数异常'];
        }
        $controllerName = $data["controllerName"];
        $start_cache_time = strtotime("-1 hour");
        $end_cache_time = time();
        $cpid = $request->cpid ?? 0;
        //查询12h之内是否查询过
        $cache = \App\Models\Chanjet\TplusReportDataCache::where(['paramDic'=>json_encode($data["condition"]),'business_type'=>$controllerName,'cpid'=>$cpid])->whereBetween('paramTime',[$start_cache_time,$end_cache_time])->first();
//        $cache = '';
        if($cache){
            return ['code'=>'000','data' => json_decode($cache['response'],true)];
        }else{
            //重新触发查询
            $res =  $this->setDataCache($data,[],$cpid);
            return $res;
        }

    }

    /**
     *  设置缓存
     *
     */
    public function setDataCache($data=[],$res=[],$cpid = 0){
        $searchItem = array_filter($data["condition"],function($item){
            if($item[0] != 'cpid') return true;
        });

        $common = new TplusCommonController();
        $res = $common->getExternalData($data['controllerName'],$data['org_id'],$searchItem,'',true);
        $responseData = is_string($res) ? json_decode($res, true) : $res;

        if (is_array($responseData) && array_key_exists('code', $responseData) && $responseData['code'] === '000') {
            $this->clearDataCacheAndInsert($data, $responseData,$cpid);
        }
        return $responseData;
    }
    /**
     *  清理报表缓存
     *
     */
    public function clearDataCacheAndInsert($data=[],$res=[],$cpid){
        $reportDataCache = \App\Models\Chanjet\TplusReportDataCache::where(['paramDic'=>json_encode($data['condition']),'business_type'=>$data["controllerName"],'cpid'=>$cpid])->first();
        $insert_data = [
            "cpid" => $cpid,
            "paramDic" => json_encode($data["condition"]),
            "business_type" => $data["controllerName"],
            "paramTime" => time(),
            "response" => json_encode($res['data'])
        ];
        if(!$reportDataCache){
            \App\Models\Chanjet\TplusReportDataCache::create($insert_data);
        }else{
            $ids = json_decode($reportDataCache['response'],true);
            $businessModel = "\\App\\Models\\Chanjet\\" . $data["controllerName"];

            $businessModel::whereIn('id',$ids)->delete();
            $reportDataCache->delete();
            \App\Models\Chanjet\TplusReportDataCache::create($insert_data);
        }
        return true;
    }

    //销售毛利分析
    public function salegrossprofitanalysis(Request $request)
    {
        $cpid = $request->cpid ?? 0;

        if (empty($cpid)) {
            return response()->json(['code' => '400', 'msg' => '请选择查询账套']);
        }

        $isgetall = $request->isgetall ?? 0;

        $start_time = isTimestamp($request->start_time)?date('Y-m-d',$request->start_time):$request->start_time;
        $end_time = isTimestamp($request->end_time)?date('Y-m-d',$request->end_time):$request->end_time;

        $settle_customer = $request->settle_customer;

        if($start_time && $end_time){
            $dic[] = ['VoucherDate',$start_time,$end_time];
        }
        if($settle_customer){
            $dic[] = ['SettleCustomer',$settle_customer];
        }

        $dic[] = ['cpid',$cpid];
        //取数来源
        $dic[] = ['FetchDataSource','00'];//00销货单 01销售出库单 02销售发票
        /***
         * 成本取值,影响到数据结果。
         *
         * 默认取 平均成本，2025.06.09
         *
         */
        $cost_value = '';
        if(isset($request->CostPriceType)){
            $dic[] = ['CostPriceType',$request->CostPriceType];//00记账成本 01参考成本 02最新成本 03 平均成本
            if($request->CostPriceType == '00') $cost_value = "记账成本";
            if($request->CostPriceType == '01') $cost_value = "参考成本";
            if($request->CostPriceType == '02') $cost_value = "最新成本";
            if($request->CostPriceType == '03') $cost_value = "平均成本";
        }
        $org_id = Company::where('id',$cpid)->value('org_id');
        $checkData = [
            'controllerName'=>'TplusSaleGrossProfitAnalysis',
            'org_id'=>$org_id,
            'condition'=>$dic
        ];
        $reportDataCheck = $this->checkReportCache($checkData,$request);
        if($reportDataCheck['code'] != '000'){
            return response()->json(['code'=>'001','msg'=>'T+数据请求失败,请检查']);
        }
        if($reportDataCheck['data']){
            $request->cache_ids = $reportDataCheck['data'];
            \App\Models\Chanjet\TplusSaleGrossProfitAnalysis::whereIn('id',$request->cache_ids)->update(["cost_value"=>$cost_value]);
        }else{
            $request->cache_ids = [];
        }

        $list = \App\Models\Chanjet\TplusSaleGrossProfitAnalysis::getList($request, $cpid, $isgetall);
        return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => $list]);
    }


    //呆滞存货分析
    public function sluggishinventoryanalysis(Request $request)
    {
        $cpid = $request->cpid;
        $isgetall = $request->isgetall ?? 0;

        $analysis_date = $request->analysis_date;
        $sluggish_standard = $request->sluggish_standard;
        if (empty($cpid)) {
            return response()->json(['code' => '400', 'msg' => '请选择查询账套']);
        }
        if(!$analysis_date) {
            return response()->json(['code'=>'001','msg'=>'分析日期查询条件不能为空']);
        }
        if(!$sluggish_standard) {
            return response()->json(['code'=>'001','msg'=>'呆滞标准查询条件不能为空']);
        }
        $dic[] = ['VoucherDate',date('Y-m-d',strtotime($analysis_date))];
        if($sluggish_standard){
            $dic[] = ['ObsoleteDays',$sluggish_standard];
        }
        $dic[] = ['cpid',$cpid];

        $org_id = Company::where('id',$cpid)->value('org_id');

        $checkData = [
            'controllerName'=>'TplusSluggishInventoryAnalysis',
            'org_id'=>$org_id,
            'condition'=>$dic
        ];
        $reportDataCheck = $this->checkReportCache($checkData,$request);
        if($reportDataCheck['code'] != '000'){
            return response()->json(['code'=>'001','msg'=>'T+数据请求失败,请检查']);
        }

        if($reportDataCheck['data']){
            $request->cache_ids = $reportDataCheck['data'];
        }else{
            $request->cache_ids = [];
        }

        $list = \App\Models\Chanjet\TplusSluggishInventoryAnalysis::getList($request, $cpid, $isgetall);
        return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => $list]);
    }

    //库存周转率分析
    public function inventoryturnoveranalysis(Request $request)
    {
        $cpid = $request->cpid;

        if (empty($cpid)) {
            return response()->json(['code' => '400', 'msg' => '请选择查询账套']);
        }
        $isgetall = $request->isgetall ?? 0;

        $start_time = $request->start_time;
        $end_time = $request->end_time;
        $analysis_method = $request->analysis_method;
        if(!$start_time || !$end_time) {
            return response()->json(['code'=>'001','msg'=>'分析日期不能为空']);
        }
        if(!$analysis_method){
            return response()->json(['code'=>'001','msg'=>'分析方式不能为空']);
        }
        //分析期间  分析方式AnalysisMethod 313按照数量   314 按照金额
        $start_time = date('Y.m',strtotime($start_time));
        $end_time = date('Y.m',strtotime($end_time));

        $dic[] = ['VoucherDate',$start_time,$end_time];
        $dic[] = ['cpid',$cpid];

        if($analysis_method){
            //313按照数量   314 按照金额
            if($analysis_method == "按照数量") $analysis_method = 313;
            if($analysis_method == "按照金额") $analysis_method = 314;
            $dic[] = ['AnalysisMethod',$analysis_method];
        }

        $org_id = Company::where('id',$cpid)->value('org_id');
        $checkData = [
            'controllerName'=>'TplusInventoryTurnoverAnalysis',
            'org_id'=>$org_id,
            'condition'=>$dic
        ];
        $reportDataCheck = $this->checkReportCache($checkData,$request);
//        print_r($reportDataCheck);
//        exit();
        if($reportDataCheck['code'] != '000'){
            return response()->json(['code'=>'001','msg'=>'T+数据请求失败,请检查']);
        }

        if($reportDataCheck['data']){
            $request->cache_ids = $reportDataCheck['data'];
        }else{
            $request->cache_ids = [];
        }

        $list = \App\Models\Chanjet\TplusInventoryTurnoverAnalysis::getList($request, $cpid, $isgetall);

        $totalField = (new BaseController($request))->fieldTotal($list['list'],
            ['quantity','stock_amount']
            ,['warehouse_name','warehouse_no']);
        $list['total'] = $totalField;
        return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => $list]);
    }

    //库龄分析
    public function inventoryageanalysis(Request $request)
    {
        $cpid = $request->cpid;

        $isgetall = $request->isgetall ?? 0;

        $analysis_date = $request->analysis_date;

        if(!$analysis_date){
            return response()->json(['code'=>'001','msg'=>'分析日期不能为空']);
        }
        $dic[] = ['VoucherDate',date('Y-m-d',strtotime($analysis_date))];
        $dic[] = ['cpid',$cpid];
        $org_id = Company::where('id',$cpid)->value('org_id');

        $checkData = [
            'controllerName'=>'TplusInventoryAgeAnalysis',
            'org_id'=>$org_id,
            'condition'=>$dic
        ];
        $reportDataCheck = $this->checkReportCache($checkData,$request);
        if($reportDataCheck['code'] != '000'){
            return response()->json(['code'=>'001','msg'=>'T+数据请求失败,请检查']);
        }

        if($reportDataCheck['data']){
            $request->cache_ids = $reportDataCheck['data'];
        }else{
            $request->cache_ids = [];
        }

        $list = \App\Models\Chanjet\TplusInventoryAgeAnalysis::getList($request, $cpid, $isgetall);
        return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => $list]);
    }


}
